//
//  JCSearchView.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/11.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCSearchView: UIView,UITextFieldDelegate {
    //取消按钮
    @IBOutlet weak var cancelButton: UIButton!
    
    //文字框
    @IBOutlet weak var textField: UITextField!

    // 文字框右边约束
    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
    
    //加载搜素框
    class func loadSearchView() -> JCSearchView {
        return NSBundle.mainBundle().loadNibNamed("JCSearchView", owner: nil, options: nil).last as! JCSearchView
    }
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//    }
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    override func awakeFromNib() {
        //设置文本框的边框
//        textField.layer.borderWidth = 1.0
//        textField.layer.borderColor = UIColor.purpleColor().CGColor
//        textField.layer.cornerRadius = 5
        
        //设置文本框左边视图
            //设置代理
        textField.delegate = self
        
        let leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        leftView.size = CGSizeMake(height, height)
        //设置内容模式
        leftView.contentMode = UIViewContentMode.Center
        textField.leftView = leftView
        //view总是可见
        textField.leftViewMode = UITextFieldViewMode.Always
    }
    
    //取消按钮点击事件
    @IBAction func cancelBtnDidClick(sender: AnyObject) {
        //退出编辑
        endEditing(true)
        textFieldRightCons.constant = 0
        UIView.animateWithDuration(0.5) { () -> Void in
           self.layoutIfNeeded()
        }
    }
    
    //UITextFieldDelegate
    func textFieldDidBeginEditing(textField: UITextField) {
        //显示 取消 按钮 (调整约束)
        textFieldRightCons.constant = self.cancelButton.width
        UIView.animateWithDuration(0.5) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
}
